package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.gio.util.UIUtils;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoBTView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoCAView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoCPView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoDPView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoMIView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoSGView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoSSView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoSTView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoTPView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoURView;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoWFView;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;
import com.jakewharton.android.viewpagerindicator.TitleProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.RelativeLayout.LayoutParams;

public class InfoFrg extends BaseFragment {
    private static final String EXTRA_PREFIX = InfoFrg.class.getName();
    public static final String EXTRA_SEL_ITEM_POS = EXTRA_PREFIX + ".sip";
    
    private ViewPager           m_infoPager;
    private InfoPagerAdapter    m_infoAdapter;
    private TitlePageIndicator  m_indicator;
    
    private Context m_cxt;
    private boolean m_for_preview = false;
    
    private static final String LOG_TAG = InfoFrg.class.getSimpleName();
    
    ////////////////////////////////////////////
    @Override public View 
    onCreateView(LayoutInflater inflater, ViewGroup vgContainer,
                 Bundle bdSavedInstanceState)
    {
        View vRoot = inflater.inflate(R.layout.info_frg, vgContainer, false);
        
        m_cxt = inflater.getContext();
        if (UIUtils.isHoneycombTablet(m_cxt)) {
        	m_for_preview = getActivity().getIntent().getExtras().getBoolean(com.a0soft.gphone.aTruffleHog.wnd.tablet.InfoWnd.EXTRA_FOR_PREVIEW, false);
        }
        else {
        	m_for_preview = getActivity().getIntent().getExtras().getBoolean(com.a0soft.gphone.aTruffleHog.wnd.phone.InfoWnd.EXTRA_FOR_PREVIEW, false);
        }
       
        m_infoPager = (ViewPager)vRoot.findViewById(R.id.infopager);
        m_infoAdapter = new InfoPagerAdapter();
        m_infoPager.setAdapter(m_infoAdapter);
        m_indicator = (TitlePageIndicator)vRoot.findViewById(R.id.indicator);
        m_indicator.setViewPager(m_infoPager);
        m_indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override public void 
            onPageSelected(int nPos)
            {
                if (aDebug.ENABLED)
                    aDebug.d(LOG_TAG, "onPageSelected() nPos=" + nPos);
                
                Intent it = new Intent();
                it.putExtra(EXTRA_SEL_ITEM_POS, nPos);
                
                BaseActivity activity = (BaseActivity)getActivity();
                activity.notifyByFragment(InfoFrg.this, it);
            }
            
            @Override public void 
            onPageScrolled(int nPos, float fPosOffset,
                           int nPosOffsetPixels)
            {
                //if (aDebug.ENABLED)
                //    aDebug.d(LOG_TAG, "onPageScrolled() nPos=" + nPos + ", fPosOffset=" + fPosOffset + ", nPosOffsetPixels=" + nPosOffsetPixels);
            }
            
            @Override public void 
            onPageScrollStateChanged(int state)
            {
                //if (aDebug.ENABLED)
                //    aDebug.d(LOG_TAG, "onPageScrollStateChanged() state=" + state);
            }
        });
        
        m_indicator.setCurrentItem(0);
        return vRoot;
    }
    
    public void
    SetCurPage(int nPos)
    {
        m_indicator.setCurrentItem(nPos);
    }
    
    public int
    GetCurPage()
    {
        return m_infoPager.getCurrentItem();
    }
    
    public void
    Refresh()
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "Refresh()");
        
        m_infoAdapter.notifyDataSetChanged();
        m_indicator.notifyDataSetChanged();
    }
    
    private class InfoPagerAdapter extends PagerAdapter implements TitleProvider 
    {
        private static final int NUM_VIEWS = 11;
        private static final int CA_VIEW_ID = 0;
        private static final int CP_VIEW_ID = 1;
        private static final int DP_VIEW_ID = 2;
        private static final int MI_VIEW_ID = 3;
        private static final int SG_VIEW_ID = 4;
        private static final int SS_VIEW_ID = 5;
        private static final int ST_VIEW_ID = 6;
        private static final int TP_VIEW_ID = 7;
        private static final int WF_VIEW_ID = 8;
        private static final int BT_VIEW_ID = 9;
        private static final int UR_VIEW_ID = 10;

        @Override
        public int getCount() 
        {
            return NUM_VIEWS;
        }

        @Override
        public String getTitle(int position) 
        {
            return getResources().getTextArray(R.array.info_indicator)[position].toString();
        }
        
        /**
         * Create the page for the given position.  The adapter is responsible
         * for adding the view to the container given here, although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate()}.
         *
         * @param container The containing View in which the page will be shown.
         * @param position The page position to be instantiated.
         * @return Returns an Object representing the new page.  This does not
         * need to be a View, but can be some other container of the page.
         */
        private void
        addINKs(DeviceInfo di, int view_id, ScrollView sview)
        {
            switch (view_id) {
            case CA_VIEW_ID:
            	addCAINKS(di, sview);
            	break;
            case CP_VIEW_ID:
                addCPINKS(di, sview);
                break;
            case DP_VIEW_ID:
                addDPINKS(di, sview);
                break;
            case MI_VIEW_ID:
                addMIINKS(di, sview);
                break;
            case SG_VIEW_ID:
                addSGINKS(di, sview);
                break;
            case SS_VIEW_ID:
                addSSINKS(di, sview);
                break;
            case ST_VIEW_ID:
                addSTINKS(di, sview);
                break;
            case TP_VIEW_ID:
                addTPINKS(di, sview);
                break;
            case UR_VIEW_ID:
                addURINKS(di, sview);
                break;
            case WF_VIEW_ID:
                addWFINKS(di, sview);
                break;
            case BT_VIEW_ID:
                addBTINKS(di, sview);
            }
        }
        
        private void
        addCAINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoCAView ca_view = new InfoCAView(m_cxt, di);
        	sview.addView(ca_view.getView(m_for_preview));
        }
        private void
        addCPINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoCPView cp_view = new InfoCPView(m_cxt, di);
            sview.addView(cp_view.getView(m_for_preview));
        }
        private void
        addDPINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoDPView dp_view = new InfoDPView(m_cxt, di);
            sview.addView(dp_view.getView(m_for_preview));          
        }
        private void
        addMIINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoMIView mi_view = new InfoMIView(m_cxt, di);
            sview.addView(mi_view.getView(m_for_preview));
        }
        private void
        addSGINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoSGView sg_view = new InfoSGView(m_cxt, di);
            sview.addView(sg_view.getView(m_for_preview));
        }
        private void
        addSSINKS(DeviceInfo di, ScrollView sview)
        {
            final InfoSSView ss_view = new InfoSSView(m_cxt, di);
            sview.addView(ss_view.getView(m_for_preview));
        }
        private void
        addSTINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoSTView st_view = new InfoSTView(m_cxt, di);
            sview.addView(st_view.getView(m_for_preview));
        }
        private void
        addTPINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoTPView tp_view = new InfoTPView(m_cxt, di);
            sview.addView(tp_view.getView(m_for_preview));
        }
        private void
        addURINKS(DeviceInfo di, ScrollView sview)
        {
        	final InfoURView ur_view = new InfoURView(m_cxt, di);
            sview.addView(ur_view.getView(m_for_preview));
        }
        private void
        addWFINKS(DeviceInfo di, ScrollView sview)
        {
            final InfoWFView wf_view = new InfoWFView(m_cxt, di);
            sview.addView(wf_view.getView(m_for_preview));
        }
        private void
        addBTINKS(DeviceInfo di, ScrollView sview)
        {
            final InfoBTView wf_view = new InfoBTView(m_cxt, di);
            sview.addView(wf_view.getView(m_for_preview));
        }
        
        @Override
        public Object instantiateItem(View collection, int position) 
        {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "instantiateItem()");
            
            DeviceInfo di = DeviceInfo.GetInstance(m_cxt);
            
            // create ScrollView
            ScrollView sv = new ScrollView(m_cxt);
            ScrollView.LayoutParams params = new ScrollView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            sv.setLayoutParams(params);
            
            addINKs(di, position, sv);
            ((ViewPager) collection).addView(sv,0);
            
            return sv;
        }

        /**
         * Remove a page for the given position.  The adapter is responsible
         * for removing the view from its container, although it only must ensure
         * this is done by the time it returns from {@link #finishUpdate()}.
         *
         * @param container The containing View from which the page will be removed.
         * @param position The page position to be removed.
         * @param object The same object that was returned by
         * {@link #instantiateItem(View, int)}.
         */
        @Override
        public void destroyItem(View collection, int position, Object view) 
        {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "destroyItem()");
            
            ((ViewPager) collection).removeView((ScrollView) view);
        }
        
        @Override
        public boolean isViewFromObject(View view, Object object) 
        {
            return view==((ScrollView)object);
        }
        
        /**
         * Called when the a change in the shown pages has been completed.  At this
         * point you must ensure that all of the pages have actually been added or
         * removed from the container as appropriate.
         * @param container The containing View which is displaying this adapter's
         * page views.
         */
        @Override
        public void finishUpdate(View arg0) {}

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() 
        {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {}
    }
}
